package v1

import (
	"ginTea/middleware"
	"ginTea/model"
	"ginTea/utils/errmsg"
	"net/http"

	"github.com/gin-gonic/gin"
)

//后端用户登陆
func Login(c *gin.Context) {
	var user model.User
	var token string
	var code int
	var id int
	c.ShouldBindJSON(&user)
	id, code = model.CheckLogin(user.Username, user.Password)

	if code == errmsg.SUCCESS {
		token, code = middleware.SetToken(user.Username)
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"token":   token,
		"success": true,
		"id":      id,
	})
}

//前端用户登陆
func LoginFront(c *gin.Context) {
	var user model.User
	var token string
	var code int
	var id int
	c.ShouldBindJSON(&user)
	id, code = model.CheckLoginFront(user.Username, user.Password)
	if code == errmsg.SUCCESS {
		token, code = middleware.SetToken(user.Username)
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"token":   token,
		"success": true,
		"id":      id,
	})
}
